home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / STRFTIME.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  6KB  |  264 lines

  1. /* This is file STRFTIME.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1989 The Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms are permitted
  12.  * provided that: (1) source distributions retain this entire copyright
  13.  * notice and comment, and (2) distributions including binaries display
  14.  * the following acknowledgement:  ``This product includes software
  15.  * developed by the University of California, Berkeley and its contributors''
  16.  * in the documentation or other materials provided with the distribution
  17.  * and in all advertising materials mentioning features or use of this
  18.  * software. Neither the name of the University nor the names of its
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  */
  25.  
  26. #if defined(LIBC_SCCS) && !defined(lint)
  27. static char sccsid[] = "@(#)strftime.c    5.8 (Berkeley) 6/1/90";
  28. #endif /* LIBC_SCCS and not lint */
  29.  
  30. #include <sys/types.h>
  31. #include <sys/time.h>
  32. #include <tzfile.h>
  33. #include <string.h>
  34.  
  35. static char *afmt[] = {
  36.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  37. };
  38. static char *Afmt[] = {
  39.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  40.     "Saturday",
  41. };
  42. static char *bfmt[] = {
  43.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  44.     "Oct", "Nov", "Dec",
  45. };
  46. static char *Bfmt[] = {
  47.     "January", "February", "March", "April", "May", "June", "July",
  48.     "August", "September", "October", "November", "December",
  49. };
  50.  
  51. static size_t gsize;
  52. static char *pt;
  53.  
  54. size_t
  55. strftime(s, maxsize, format, t)
  56.     char *s;
  57.     char *format;
  58.     size_t maxsize;
  59.     struct tm *t;
  60. {
  61.     size_t _fmt();
  62.  
  63.     pt = s;
  64.     if ((gsize = maxsize) < 1)
  65.         return(0);
  66.     if (_fmt(format, t)) {
  67.         *pt = '\0';
  68.         return(maxsize - gsize);
  69.     }
  70.     return(0);
  71. }
  72.  
  73. static size_t
  74. _fmt(format, t)
  75.     register char *format;
  76.     struct tm *t;
  77. {
  78.     for (; *format; ++format) {
  79.         if (*format == '%')
  80.             switch(*++format) {
  81.             case '\0':
  82.                 --format;
  83.                 break;
  84.             case 'A':
  85.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  86.                     return(0);
  87.                 if (!_add(Afmt[t->tm_wday]))
  88.                     return(0);
  89.                 continue;
  90.             case 'a':
  91.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  92.                     return(0);
  93.                 if (!_add(afmt[t->tm_wday]))
  94.                     return(0);
  95.                 continue;
  96.             case 'B':
  97.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  98.                     return(0);
  99.                 if (!_add(Bfmt[t->tm_mon]))
  100.                     return(0);
  101.                 continue;
  102.             case 'b':
  103.             case 'h':
  104.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  105.                     return(0);
  106.                 if (!_add(bfmt[t->tm_mon]))
  107.                     return(0);
  108.                 continue;
  109.             case 'C':
  110.                 if (!_fmt("%a %b %e %H:%M:%S %Y", t))
  111.                     return(0);
  112.                 continue;
  113.             case 'c':
  114.                 if (!_fmt("%m/%d/%y %H:%M:%S", t))
  115.                     return(0);
  116.                 continue;
  117.             case 'e':
  118.                 if (!_conv(t->tm_mday, 2, ' '))
  119.                     return(0);
  120.                 continue;
  121.             case 'D':
  122.                 if (!_fmt("%m/%d/%y", t))
  123.                     return(0);
  124.                 continue;
  125.             case 'd':
  126.                 if (!_conv(t->tm_mday, 2, '0'))
  127.                     return(0);
  128.                 continue;
  129.             case 'H':
  130.                 if (!_conv(t->tm_hour, 2, '0'))
  131.                     return(0);
  132.                 continue;
  133.             case 'I':
  134.                 if (!_conv(t->tm_hour % 12 ?
  135.                     t->tm_hour % 12 : 12, 2, '0'))
  136.                     return(0);
  137.                 continue;
  138.             case 'j':
  139.                 if (!_conv(t->tm_yday + 1, 3, '0'))
  140.                     return(0);
  141.                 continue;
  142.             case 'k':
  143.                 if (!_conv(t->tm_hour, 2, ' '))
  144.                     return(0);
  145.                 continue;
  146.             case 'l':
  147.                 if (!_conv(t->tm_hour % 12 ?
  148.                     t->tm_hour % 12 : 12, 2, ' '))
  149.                     return(0);
  150.                 continue;
  151.             case 'M':
  152.                 if (!_conv(t->tm_min, 2, '0'))
  153.                     return(0);
  154.                 continue;
  155.             case 'm':
  156.                 if (!_conv(t->tm_mon + 1, 2, '0'))
  157.                     return(0);
  158.                 continue;
  159.             case 'n':
  160.                 if (!_add("\n"))
  161.                     return(0);
  162.                 continue;
  163.             case 'p':
  164.                 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
  165.                     return(0);
  166.                 continue;
  167.             case 'R':
  168.                 if (!_fmt("%H:%M", t))
  169.                     return(0);
  170.                 continue;
  171.             case 'r':
  172.                 if (!_fmt("%I:%M:%S %p", t))
  173.                     return(0);
  174.                 continue;
  175.             case 'S':
  176.                 if (!_conv(t->tm_sec, 2, '0'))
  177.                     return(0);
  178.                 continue;
  179.             case 'T':
  180.             case 'X':
  181.                 if (!_fmt("%H:%M:%S", t))
  182.                     return(0);
  183.                 continue;
  184.             case 't':
  185.                 if (!_add("\t"))
  186.                     return(0);
  187.                 continue;
  188.             case 'U':
  189.                 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
  190.                     2, '0'))
  191.                     return(0);
  192.                 continue;
  193.             case 'W':
  194.                 if (!_conv((t->tm_yday + 7 -
  195.                     (t->tm_wday ? (t->tm_wday - 1) : 6))
  196.                     / 7, 2, '0'))
  197.                     return(0);
  198.                 continue;
  199.             case 'w':
  200.                 if (!_conv(t->tm_wday, 1, '0'))
  201.                     return(0);
  202.                 continue;
  203.             case 'x':
  204.                 if (!_fmt("%m/%d/%y", t))
  205.                     return(0);
  206.                 continue;
  207.             case 'y':
  208.                 if (!_conv((t->tm_year + TM_YEAR_BASE)
  209.                     % 100, 2, '0'))
  210.                     return(0);
  211.                 continue;
  212.             case 'Y':
  213.                 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
  214.                     return(0);
  215.                 continue;
  216. #if 0
  217.             case 'Z':
  218.                 if (!t->tm_zone || !_add(t->tm_zone))
  219.                     return(0);
  220.                 continue;
  221. #endif
  222.             case '%':
  223.             /*
  224.              * X311J/88-090 (4.12.3.5): if conversion char is
  225.              * undefined, behavior is undefined.  Print out the
  226.              * character itself as printf(3) does.
  227.              */
  228.             default:
  229.                 break;
  230.         }
  231.         if (!gsize--)
  232.             return(0);
  233.         *pt++ = *format;
  234.     }
  235.     return(gsize);
  236. }
  237.  
  238. static
  239. _conv(n, digits, pad)
  240.     int n, digits;
  241.     char pad;
  242. {
  243.     static char buf[10];
  244.     register char *p;
  245.  
  246.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  247.         *p-- = n % 10 + '0';
  248.     while (p > buf && digits-- > 0)
  249.         *p-- = pad;
  250.     return(_add(++p));
  251. }
  252.  
  253. static
  254. _add(str)
  255.     register char *str;
  256. {
  257.     for (;; ++pt, --gsize) {
  258.         if (!gsize)
  259.             return(0);
  260.         if (!(*pt = *str++))
  261.             return(1);
  262.     }
  263. }
  264.